#include"offinemessagemodel.hpp"
#include"CommonConnectionPool.hpp"

#include<iostream>
using namespace std;

// 存储用户离线消息
void OfflineMsgModel::insert(int userid, string msg)
{
    // 1. 组装sql语句
    char sql[1024] ={0};
    sprintf(sql,"insert into offlinemessage values(%d,'%s')",userid,msg.c_str());

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getConnection();
    mysql->update(sql); 
 
}

// 删除用户离线消息
void OfflineMsgModel::remove(int userid)
{
    // 1. 组装sql语句
    char sql[1024] ={0};
    sprintf(sql,"delete from offlinemessage where userid=%d",userid);

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getConnection();
    mysql->update(sql); 
}

// 查询用户的离线消息
vector<string> OfflineMsgModel::query(int userid)
{
    // 1. 组装sql语句
    char sql[1024] ={0};
    sprintf(sql,"select message from offlinemessage where userid=%d",userid);

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getConnection();
    vector<string> vec;

    MYSQL_RES * res = mysql->query(sql);
    if(res != nullptr)
    {
        MYSQL_ROW row;
        //把userid 用户的所有离线消息 都放入 vec中
        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            vec.push_back(row[0]);
        }//不断得拿
        //释放资源
        mysql_free_result(res);  
    }

    return vec; 
}